// pages/shareFriends/shareFriends.js
const app = getApp();
const util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showMask: false,//添旺弹窗
    cannotInput: false,
    moneyList:[0.01,19.99,38.88,68.88,88.88,188.88],
    money:9.99,
    userInfo: ""
  },

  /**
   * 金额选择
   */
  choose:function(event){
    let money = event.currentTarget.dataset.money
    this.setData({
      money: money
    })
  },


  // 为他添旺显示弹窗
  addLuck:function(res){
    // console.log("为他添旺")
    var that = this
    var animation = wx.createAnimation({
      duration: 800,
      timingFunction: 'ease'
    })
    that.animation = animation
    animation.translateY(-503).opacity(0.5).step()

    that.setData({
      maskAnim: animation.export(),
      showMask: true
    })
    setTimeout(function () {
      animation.translateY(0).opacity(1).step()
      that.setData({
        maskAnim: animation.export()
      })
    }, 200)

    
  },
  preventTouchMove:function(res){

  },
//暂不支持取消弹窗
  cancel: function(res){
    var that = this
    var animation = wx.createAnimation({
      duration: 800,
      timingFunction: 'ease'
    })
    that.animation = animation
    animation.translateY(-503).opacity(0.5).step()

    that.setData({
      maskAnim: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).opacity(1).step()
      that.setData({
        maskAnim: animation.export(),
        showMask: false
      })
     
    }, 500)
  

  },
  //立即添旺
  addLuckSucess:function(res){
    let that = this
    let getTitleDetailUrl = app.globalData.baseUrl + "pay"
    let data = {
      openid: app.globalData.openId,
      orderId: that.data.orderId,
      money: that.data.money,
      cid: app.globalData.cid
    }

    util.requestLoading(getTitleDetailUrl, data, "GET").then(res => {
      wx.showLoading()
      wx.requestPayment({
        timeStamp: res.data.timeStamp,
        nonceStr: res.data.nonceStr,
        package: res.data.package,
        signType: 'MD5',
        paySign: res.data.paySign,
        success: function (event) {
          // console.log("支付成功")
          getTitleDetailUrl = app.globalData.baseUrl + "addWang"
          data = {
            orderId: that.data.orderId,
            cid: app.globalData.cid,
            wangNum: that.data.money,
            note: that.data.notes
          }
          util.requestLoading(getTitleDetailUrl, data, "GET").then(res => {
            // console.log(res)
            wx.hideLoading()
            if (res.data.status==0){
              wx.showToast({
                title: '添旺成功',
                icon: 'success',
                duration: 1500
              });
              setTimeout(function () {
                wx.navigateTo({
                  url: '/pages/addluckSuccess/addluckSuccess?cid=' + that.data.cid,
                })
              }, 1500)
            }
          })
          
        },
        fail: function (error) {
          // console.log("支付失败")
          // console.log(error)
          wx.showToast({
            title: '添旺失败',
            icon: 'loading',
            duration: 1500
          });
          setTimeout(function () {
            wx.switchTab({
              url: '/pages/main/main',
            })
          }, 1500)
        },
        complete: function () {
          // complete
          // console.log("pay complete")
        }
      });
    })
    
  },
  // 留言寄语
  noteInput:function(res){
  //  console.log(res.detail.value)
    this.setData({
      notes: res.detail.value
    })
      //不能超过20字
    if (res.detail.value.length==20){
          this.setData({
            cannotInput: true
          })
    }else{
      this.setData({
        cannotInput: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let name = options.name
    let orderId = options.orderId
    let cid = options.cid
    app.globalData.aid = options.cid
    let birthday = options.birthday
    let type = options.type
    // console.log(type)
    this.setData({
      name: name,
      orderId: orderId,
      cid: cid,
      // birthday: birthday,
      type: type
    })
    //生成订单
    let getTitleDetailUrl = ""
    if (birthday==0){
      getTitleDetailUrl = app.globalData.baseUrl + "inviteFriends"
    }else{
      getTitleDetailUrl = app.globalData.baseUrl + "birthdayInvite"
    }
    let data = {
      cid: cid,
      orderId: orderId,
      name: name
    }
    util.requestLoading(getTitleDetailUrl, data, "GET").then(res => {
      // console.log(res)
      this.setData({
        detail: res.data.detail,
        friendsList: res.data.friendsList,
        needWangNum: res.data.needWangNum,
        hasWangNum: res.data.hasWangNum,
        pic: res.data.pic,
        title: res.data.title,
        birthdayNote: res.data.birthdayNote,
        wxLoc: res.data.wxLoc,
        birthdayPic: res.data.birthdayPic
      })
    })
  },


  getUserInfo: function () {
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // console.log(res)
              // 可以将 res 发送给后台解码出 unionId
              app.globalData.userInfo = res.userInfo
              this.setData({
                userInfo: res.userInfo
              })
              let getTitleDetailUrl = app.globalData.baseUrl + "updateCustomerMsg"
              let data = {
                openid: wx.getStorageSync('openId'),
                nickName: res.userInfo.nickName,
                wxLoc: res.userInfo.avatarUrl
              }
              util.requestLoading(getTitleDetailUrl, data, "GET").then(res => {
                // console.log(res)
              })

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
              var that = this
              var animation = wx.createAnimation({
                duration: 800,
                timingFunction: 'ease'
              })
              that.animation = animation
              animation.translateY(-503).opacity(0.5).step()

              that.setData({
                maskAnim: animation.export(),
                showMask: true
              })
              setTimeout(function () {
                animation.translateY(0).opacity(1).step()
                that.setData({
                  maskAnim: animation.export()
                })
              }, 200)
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const ctx = wx.createCanvasContext('myCanvas')
    var x = 200
    // Create linear gradient
    const grd = ctx.createLinearGradient(0, 0, 200, 0)
    grd.addColorStop(0, '#FFF7B9')
    grd.addColorStop(0.5, '#F0222E')
    grd.addColorStop(1, '#AB426F')
    // Fill with gradient
    ctx.setFillStyle(grd)
    ctx.fillRect(0, 0, x, 15)
    ctx.draw()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})